package xl.struct
{

	/**
	 *
	 * @author xiling
	 *
	 */
	public class XArray
	{
		private var _array:Array;

		public var length:int = 0;
		public var position:int = 0;
		private var delim:String = "|";

		public function XArray(data:String, delim:* = "|")
		{
			this.delim = delim;
			init(data, delim);
		}

		public function init(data:String, delim:* = "|"):void
		{
			data = data.replace(/[\r\t\n]/g, "");
			_array = data.split(delim);
			length = _array.length;
			position = 0;
		}

		public function get countAvailable():int
		{
			return length - position;
		}

		public function read(index:int = -1):*
		{
			if (index == -1)
				return _array[position++];
			position = index + 1;
			return _array[index];
		}

		public function readInt(index:int = -1):int
		{
			return int(read(index));
		}

		public function readUint(index:int = -1):uint
		{
			return uint(read(index));
		}

		public function readString(index:int = -1):String
		{
			return String(read(index));
		}

		public function readFloat(index:int = -1):Number
		{
			return Number(read(index));
		}

		public function readMulti(len:int = 0, delim:* = "|"):String
		{
			var maxLen:int = _array.length - position;
			len = (len == 0 ? maxLen : Math.min(maxLen, len));
			if (len < 1)
				return "";

			var oldPosition:int = position;
			position += len;
			return _array.slice(oldPosition, position).join(delim);
		}

		public function pop(len:int = 1):Array
		{
			if (len < 1)
				return null;

			var tempArray:Array = [];

			for (var i:int = 0; i < len; i++)
			{
				tempArray[i] = _array.pop();
				tempArray.reverse();
			}

			position = 0;

			return tempArray;

		}

		public function shift(len:int = 1):Array
		{

			if (len < 1)
				return null;

			var tempArray:Array = [];

			for (var i:int = 0; i < len; i++)
			{
				tempArray[i] = _array.shift();
			}

			position = 0;

			return tempArray;

		}

		public function toArray():Array
		{
			return _array;
		}

		public function toString():String
		{
			return _array.join(delim);
		}
	}
}
